<template>
  <div>
    <el-form ref="form" :rules="rules" class="dept-form" :model="deptForm" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="deptForm.manager" placeholder="请选择活动区域">
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="活动形式" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="onSubmit">立即创建</el-button>
        <el-button size="mini">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    //   自定义校验部门名称
    const validName = (rules, value, callback) => {
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      //   if (this.isEdit) {
      //     existNameList = this.originList.filter(item => item.id !== this.parentId).map(item => item.name)
      //   }
      if (this.isEdit) {
        //   获取编辑本身
        const dept = this.originList.find(item => item.id === this.parentId)
        // 获取到pid
        const pid = dept.pid
        // 使用filter 筛选出pid 一样的兄弟节点 但是需要排除本身
        // 使用map 方法将兄弟结点的name组成一个新的数组
        existNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }
      existNameList.includes(value) ? callback(new Error('部门名称' + value + '已存在')) : callback()
    }
    //   自定义部门编码校验
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      existCodeList.includes(value) ? callback(new Error('修改的' + value + '已存在')) : callback()
    }
    return {
      deptForm: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  created() {
    //   获取负责人
    this.getEmployeeSimple()
    // 查找部门
    this.getDepartDetail()
  },
  methods: {
    //   通过id查找部门
    async getDepartDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        // console.log(res)
        this.deptForm = res.data
      }
    },

    //   获取负责人列表
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employees = res.data
    //   console.log(res.data)
    },
    onSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        if (this.isEdit) {
        //   编辑
          await updateDepartments(this.deptForm)
          // console.log(res)
          this.$emit('closeSubmit')
        } else {
        //   新增
          const params = { ...this.deptForm, pid: this.parentId }
          await addDepartments(params)
        // console.log(res)
        }
        //   弹框隐藏
        this.$emit('close')
        //   添加成功提示
        this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
      //  刷新页面
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.dept-form {
  padding: 0px 80px 0px 30px;
}

</style>
